export const pollVideoTaskStatus = async (taskId) => {
  try {
    const response = await request({
      url: `/api/video/task/${taskId}/status`,
      method: 'get'
    })
    return response
  } catch (error) {
    console.error('获取任务状态失败:', error)
    throw error
  }
}

export const generateVideo = async (data) => {
  try {
    const response = await request({
      url: '/api/media-resources/generate-video',
      method: 'post',
      data
    })
    return response.data
  } catch (error) {
    console.error('生成视频失败:', error)
    throw error
  }
}

export const getVideoTaskStatus = async (taskId) => {
  try {
    const response = await request({
      url: `/api/media-resources/task/${taskId}/status`,
      method: 'get'
    })
    return response.data
  } catch (error) {
    console.error('获取任务状态失败:', error)
    throw error
  }
}

export const getVideoUrl = async (taskId) => {
  try {
    const response = await request({
      url: `/api/media-resources/task/${taskId}/url`,
      method: 'get'
    })
    return response.data
  } catch (error) {
    console.error('获取视频URL失败:', error)
    throw error
  }
}

export const startPollingTaskStatus = (taskId, onSuccess, onError, interval = 5000) => {
  const poll = async () => {
    try {
      const status = await getVideoTaskStatus(taskId)
      if (status === 'SUCCESS') {
        const videoUrl = await getVideoUrl(taskId)
        onSuccess(videoUrl)
      } else if (status === 'FAILED') {
        onError('视频生成失败')
      } else {
        setTimeout(poll, interval)
      }
    } catch (error) {
      onError(error.message)
    }
  }
  poll()
} 